package org.springbus.ff.tool;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TimeMarkUtils {

    /**
     * Convert a [[hh:]mm:]ss[.xxx] timemark into seconds
     *
     * @param {String} timemark timemark string
     * @return Number
     * @private
     */
    public static Float timemarkToSeconds(String timemark) {

        if (timemark.indexOf(':') == -1 && timemark.indexOf('.') >= 0) {
            return Float.parseFloat(timemark);
        }

        String[] timeParts = timemark.split(":");
        List<String> parts = new ArrayList<>(Arrays.asList(timeParts));

        // add seconds
        float secs = Float.parseFloat(parts.remove(parts.size()-1));

        if (!parts.isEmpty()) {
            // add minutes
            secs += Integer.parseInt(parts.remove(parts.size()-1)) * 60;
        }

        if (!parts.isEmpty()) {
            // add hours
            secs += Integer.parseInt(parts.remove(parts.size()-1)) * 3600;
        }

        return secs;
    }

}
